package com.shuyihui.identity

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.shuyihui.identity.bean.Spec
import com.shuyihui.identity.bean.SpecCombination

/**
 * 创建日期 2022/3/10
 * 描述：
 */

class SpecAdapter(private val specList: List<Spec>, private val specCombinationList: List<SpecCombination>, private val matrix: Array<IntArray>) :
    RecyclerView.Adapter<SpecAdapter.ViewHolder>() {

    private var canSelectSpec = SpecSkuUtil.getCanSelectSpec(SpecSkuUtil.allOr(matrix), specList)
    private val alreadySelectSpec = Array(specList.size) { "" }

    interface OnClickSpecListener {
        fun clickSpec(enabled: Boolean, id: String? = null)
    }

    private var onClickSpecListener: OnClickSpecListener? = null

    fun setOnClickSpecListener(listener: OnClickSpecListener) {
        this.onClickSpecListener = listener
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val typeTv: TextView = itemView.findViewById(R.id.tv_type)
        val specLayout: LinearLayout = itemView.findViewById(R.id.ll_spec)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_spec, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val spec = specList[position]
        holder.typeTv.text = spec.title
        holder.specLayout.removeAllViews()
        spec.list.forEachIndexed { index, text ->
            val specTv = LayoutInflater.from(holder.itemView.context).inflate(R.layout.item_spec_child, holder.specLayout, false) as TextView
            specTv.text = text

            if (alreadySelectSpec[position] == text || canSelectSpec[position][index] == 1) {
                if (alreadySelectSpec[position] == text) {
                    specTv.isSelected = true
                }
                specTv.isEnabled = true
                specTv.setOnClickListener {
                    if (specTv.isSelected) {
                        specTv.isSelected = false
                        updateCanSelectList(position, "")
                    } else {
                        for (i in 0 until holder.specLayout.childCount) {
                            val childAt = holder.specLayout.getChildAt(i) as TextView
                            if (childAt.isSelected) {
                                childAt.isSelected = false
                            }
                        }
                        specTv.isSelected = true
                        updateCanSelectList(position, text)
                    }
                }
            } else {
                specTv.isSelected = false
                specTv.isEnabled = false
                specTv.setOnClickListener(null)
            }

            holder.specLayout.addView(specTv)
        }
    }

    private fun updateCanSelectList(position: Int, needAddSpec: String) {
        val rows = arrayListOf<Int>()
        alreadySelectSpec[position] = needAddSpec

        if (alreadySelectSpec.contains("")) {
            onClickSpecListener?.clickSpec(false)
        } else {
            for (combination in specCombinationList) {
                if (combination.specs.sorted() == alreadySelectSpec.sorted()) {
                    onClickSpecListener?.clickSpec(true, combination.id)
                    break
                }
            }
        }

        alreadySelectSpec.forEachIndexed { index, it ->
            if (it.isNotEmpty()) {
                var total = 0
                specList.forEachIndexed { i, spec ->
                    if (i == index) {
                        rows.add(total + spec.list.indexOf(it))
                    }
                    total += spec.list.size
                }
            }
        }

        canSelectSpec = SpecSkuUtil.getCanSelectSpec(SpecSkuUtil.rowAnd(rows, matrix), specList)
        notifyDataSetChanged()
    }

    override fun getItemCount() = specList.size
}

